package action;

import controller.Action;
import dao.BD;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

public class RelatorioDisciplinaPorCursoAction extends Action{

    @Override
    public void prepararOperacao(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        RequestDispatcher view = request.getRequestDispatcher("/disciplinaPorCurso.jsp");
        try {
            view.forward(request, response);
        } catch (IOException ex) {
            throw new ServletException(ex);
        }
    }
    @Override
    public void confirmarOperacao(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        Connection conexao = null;
        try {
            conexao = BD.getConexao();
            HashMap parametros = new HashMap();
            parametros.put("PAR_codCurso", Integer.parseInt(request.getParameter("txtCodCurso")));
            InputStream relatorio = getClass().getResourceAsStream("/report/disciplinaXCurso.jasper");                   
            JasperPrint jp = JasperFillManager.fillReport(relatorio, parametros, conexao);
            byte[] relat = JasperExportManager.exportReportToPdf(jp);
            response.setHeader("Content-Disposition", "attachment;filename=disciplinaXCurso.pdf");
            response.setContentType("application/pdf");
            response.getOutputStream().write(relat);
        } catch (SQLException ex) {
            throw new ServletException(ex);
        } catch (ClassNotFoundException ex) {
            throw new ServletException(ex);
        } catch (JRException ex) {
            throw new ServletException(ex);
        } catch (IOException ex) {
            throw new ServletException(ex);
        } finally {
            try {
                if (!conexao.isClosed()) {
                    conexao.close();
                }
            } catch (SQLException ex) {
            }
        }
        
    }
    
}
